var co = require('js/common.js')
var _this, _function
App({
   aData: {//全局数据
      app: {
         appid: 'wxa9fe10d8217d45c9',//'wx66040e1a16ab65d4',//
         secret: '8bcbe1911bf71885a366bbcf386de0a5'//'7c19138e858420ef1125f2d7752e5e83'//
      },
      reg_from: 'repair_wxapp',
      p: {
         latitude: 26.1002,//26.100216398141626,
         longitude: 119.2958,//119.29580735449221,
      },
      userInfo: {},
      inviter: { //邀请人
         inviter_uid: 'repair_wxapp',
         inviter_oid: 'repair_wxapp'
      },
      mode: 'master'
   },

   onLaunch: function () {
      _this = this
      console.log('【_this】', _this)
   },

   getUserInfo: function (_callback, whatUse) {
      _function = _callback
      wx.login({
         success: function (res) {
            var param = { code: res.code }
            _this.getUserInfo2(param, whatUse)
         },
         fail: function (res) { },
      })
   },

   getUserInfo2: function (param, whatUse) {
      param = co.addValueForKey(param, _this.aData.app)
      co.getShareApi('user/mg/getuserinfo', param, function (res) {
         wx.setStorageSync('session_key', res.data.session_key)
         if (whatUse == 'getPhoneNumber') {
            _function('reget_session_key')
         }
         delete res.data.session_key
         if (res.code == 0) {
            wx.setStorageSync('userInfo', res.data)
            _function(res) //返回有'identity'
         } else {
            _this.getUserInfo3(res)
         }
      })
   },
   //状态：29300未注册时执行
   getUserInfo3: function (result) {
      wx.getUserInfo({
         withCredentials: true,
         lang: '',
         success: function (res) {
            if (result.code == 29300) {
               _function({ data: res.userInfo })   //先返回无'identity'
               var keys = ['unionid', 'openid']    //加两key
               res.userInfo = co.addValueForKey(res.userInfo, result.data, keys)
               _this.regUserInfo(res.userInfo) //注册
            } else if (result.code == 71748) {
               _this.decryptUserInfo(res)
            }
         },
         fail: function (res) {
            console.log('【用户拒绝授权res】', res)
            _this.aData.userInfo = result
         },
      })
   },
   //解密用户信息
   decryptUserInfo: function (res) {
      var param = {
         appid: this.aData.app.appid,
         session_key: wx.getStorageSync('session_key'),
         encryptedData: res.encryptedData,
         iv: res.iv
      }
      co.getShareApi('user/mg/decrypt', param, function (res) {
         console.log('【decryptUserInfo解密结果】', res);
         if (res.data.watermark)
            delete res.data.watermark
         _function({ data: res.data })   //先返回无'identity'
         _this.regUserInfo(res.data) //注册
      })
   },
   //自动注册
   regUserInfo: function (userInfo) {
      var param = userInfo
      param = co.addValueForKey(param, this.aData.p)        //加经纬度
      param = co.addValueForKey(param, this.aData.inviter)  //加经纬度
      param.appid = this.aData.app.appid
      param.reg_from = this.aData.reg_from
      co.getShareApi('user/mg/regwxinfo', param, function (res) {
         if (res.code == 0 || res.code == 29311) {
            wx.setStorageSync('userInfo', res.data)
            console.log('【自动注册成功res】', res)
            _function(res)//再返回有'identity'
         }
      })
   },
   //保存日志
   saveUserLog: function (param) {
      var keys = ['unionid', 'openid', 'nickName', 'avatarUrl', 'gender']
      var param_log = co.addValueForKey({}, param, keys)
      param_log = co.addValueForKey(param_log, this.aData.p)
      param_log = co.addValueForKey(param_log, this.aData.inviter)
      param_log.appid = this.aData.app.appid
      param_log.reg_from = this.aData.reg_from
      co.getShareApi('user/Logs/saveUserLog', param_log, function (res) {
         console.log('【保存日志res】', res)
      })
   },
   //播放声音
   playMp3: function (audioUrl) {//有新消息声音提醒
      wx.playBackgroundAudio({
         dataUrl: audioUrl,
         success: function (res) {
            console.log('play success: ', res)
         },
         fail: function (res) {
            console.log('play fail: ', res)
         },
      })
   },

   setNoticeUpdateUserInfo: function (callback) {
      this.personCenterCallback = callback
   },

   noticeUpdateUserInfo: function (res) {
      if (typeof (this.personCenterCallback) == 'function') {
         this.personCenterCallback(res)
      }
   },

   setHomePageShowAvatarUrlNotice: function (callback) {
      this.homePageCallback = callback
   },

   noticeHomePageShowAvatarUrl: function (res) {
      if (typeof (this.homePageCallback) == 'function') {
         this.homePageCallback(res)
      }
   },
})
